/*
 * Staff.java
 *
 * Created on 11. Juni 2007, 03:32
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package sbc.fact;

import corso.lang.*;

import java.util.*;
import sbc.corso.CorsoConnectionHolder;
import sbc.repos.*;
import sbc.util.*;
/**
 *
 * @author mustermark
 */
public class Storage extends CorsoConnectionHolder implements CorsoShareable, Identifiable  {
    
    private List<CorsoVarOid> moduleOids;
    private List<Board> boards = new ArrayList<Board>();
    private Factory fact;
    private String uid;
    private static int cnt;
    private CorsoVarOid storageOid;
    /**
     * Creates a new instance of Staff
     */
    /*public Storage(Factory f) {
        moduleOids = new ArrayList<CorsoVarOid>();
        fact = f;
        uid = f.getUID() + ":Storage_" + cnt++;
        CorsoVarOid oid;
        // create object for registration **************************************
        Registration deliveryObj = new Registration();

        // create a named oid or get it, if it yet exists **********************
        CorsoVarOid deliveryOid = null;
    
        deliveryOid = con.getOrCreateNamedVarOid (con.getCurrentStrategy(), "DeliveryObject", null);

        // create notification on this object **********************************
        Vector notifVec = new Vector ();

        CorsoNotificationItem delivNotifItem = new CorsoNotificationItem (deliveryOid, CorsoNotificationItem.INITIALIZE_WITH_CURRENT_TIMESTAMP, CorsoNotificationItem.CURRENT_TIMESTAMP);

        notifVec.addElement (delivNotifItem);

        CorsoNotification notification = con.createNotification (notifVec, con.getCurrentStrategy());

        CorsoData data = con.createData();

        System.out.println ("U may start to deliver now....");

        for (;;) {
            // now waiting for writing regObj or a data object *******************
            System.out.println ("waiting for registration or data");
            CorsoNotificationItem fired = notification.start (CorsoConnection.INFINITE_TIMEOUT, data);

            if (fired != null) {
                if (fired.varOid ().equals(deliveryOid)) {
                    // a new data object was registered ******************************
                    // so read the new oid and add it to the notification ************
                    System.out.println ("someone wants to register a new object");

                    // get object from oid *******************************************
                    deliveryOid.readShareable (deliveryObj, null, CorsoConnection.NO_TIMEOUT);

                    // get new data oid from object **********************************
                    CorsoVarOid newDataOid = deliveryObj.oid;

                    // create notification item on the new data oid ******************
                    CorsoNotificationItem newDelivNotifItem = new CorsoNotificationItem (newDataOid, 0, CorsoNotificationItem.CURRENT_TIMESTAMP);

                    // add notification item to notification *************************
                    notification.addItem (newDelivNotifItem, CorsoConnection.NO_TIMEOUT);
                    System.out.println ("ok, registration was sucessfull");

                    int number = notification.getItems ().size ();
                    System.out.print ("i am listening to 1 registration object ");
                    System.out.println ("and "+ (number - 1) + " data objects");
                    System.out.println ("-------------------------------");
                } else {
                    // a registered data object was written **************************
                    System.out.println ("someone has written the data object");

                    // read the data and display it **********************************
                    //Sample8Data dataObj = new Sample8Data ();
                    Board boardObj = new Board();
                    fired.varOid ().readShareable (boardObj, null, CorsoConnection.NO_TIMEOUT);
                    //dataObj.print ();
                    boards.add(boardObj);
                }
            }
        }
    }*/
    
    
    /**
     * reads an instance of Storage
     */
    protected Storage(CorsoVarOid oid) {
        System.out.println ("Read Storage = " + oid);
        try {
            oid.readShareable(this, null, CorsoConnection.INFINITE_TIMEOUT);
        } catch (CorsoException e) {
            e.printStackTrace();
        }
    }
    
    public void write (CorsoData data) throws CorsoDataException
    {
        System.out.println("wrinting storage");
        data.putStructTag ("Storage", moduleOids.size ());
        for (int i = 0; i < moduleOids.size(); i++) data.putShareable ((CorsoVarOid)moduleOids.get(i));
        
    }

    public String getUID() { return uid; }

    //------------------------------------------------------------------------
    // read
    //------------------------------------------------------------------------
    public void read (CorsoData data) throws CorsoDataException
    {
        System.out.println("reading storage");
        StringBuffer structName = new StringBuffer ();
        int arity = data.getStructTag (structName);

        moduleOids = new ArrayList<CorsoVarOid>();
        for (int i = 0; i < arity; i++)
        {
            CorsoVarOid oid = new CorsoVarOid ();
            data.getShareable (oid);
            System.out.println ("Next Module = " + oid);
            moduleOids.add(oid);
        }
        
    }
    
    public void accept(Module m) {
        //landebahnenOids = new Vector (nLandebahnen);
        CorsoVarOid oid;
        try {

            oid = con.createVarOid (con.getCurrentStrategy ());
            oid.writeShareable (m, CorsoConnection.INFINITE_TIMEOUT);
            moduleOids.add (oid);
        
        } catch (CorsoException eSys1) {   
            System.out.println (eSys1);
            System.exit (-2);
        }
    }
    
    public int getDeliveryCount() { return moduleOids.size(); }
    
    public List<CorsoVarOid> getModuleOids() { return moduleOids; }
     public void createOrGetOids() {
        
        try {
            storageOid = super.getDefaultNamedVarOid (getUID());
            System.out.println ("Storage found:" + storageOid);
            
        }
        catch (CorsoException e1)
        {
            System.out.println ("Storage not found");
            try{
                storageOid = super.createDefaultNamedVarOid(getUID());
                storageOid.writeShareable (this, CorsoConnection.INFINITE_TIMEOUT);
                System.out.println ("New Storage created: " + storageOid);
            } catch (CorsoException e2) {
                System.out.println ("Cannot create named oid " + e2);
                e2.printStackTrace();
                System.exit (-4);
                
            }
        }
        
        
    
    }
     
    public CorsoVarOid getOid() { return storageOid; }
   
}

